package com.jinxiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinxiangyang.domain.Course;
import com.jinxiangyang.domain.CourseDescription;
import com.jinxiangyang.domain.Subject;
import com.jinxiangyang.domain.Teacher;
import com.jinxiangyang.domain.vo.CourseFormVo;
import com.jinxiangyang.domain.vo.CoursePublishVo;
import com.jinxiangyang.domain.vo.CourseQueryVo;
import com.jinxiangyang.mapper.CourseMapper;
import com.jinxiangyang.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {


    @Autowired
    private  TeacherService teacherService;
    @Autowired
    private  SubjectService subjectService;

    @Autowired
    private CourseDescriptionService courseDescriptionService;


    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    /**
     * 点播课程列表
     * @param pageParam
     * @param courseQueryVo
     * @return
     */

    @Override
    public  Map<String,Object>  findPageCourse(Page<Course> pageParam, CourseQueryVo courseQueryVo) {
        String title = courseQueryVo.getTitle();
        Long teacherId = courseQueryVo.getTeacherId();
        Long subjectId = courseQueryVo.getSubjectId();
        Long subjectParentId = courseQueryVo.getSubjectParentId();


        LambdaQueryWrapper<Course> wrap = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(title)) {
            wrap.like(Course::getTitle,title);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            wrap.eq(Course::getTeacherId,teacherId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            wrap.eq(Course::getSubjectId,subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrap.eq(Course::getSubjectParentId,subjectParentId);
        }


        Page<Course> page = baseMapper.selectPage(pageParam, wrap);

        List<Course> records = page.getRecords();
        long total = page.getTotal();
        long pages = page.getPages();

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("pages",pages);
        map.put("records",records);
        records.forEach(item ->{
            this.getNameById(item);
        });
        return map;
    }

    /**
     *
     * 添加用户信息并且放回课程id值
     * @param courseFormVo
     * @return
     */
    @Override
    public Long saveCourseInfo(CourseFormVo courseFormVo) {
        Course courseInfo = new Course();
        BeanUtils.copyProperties(courseFormVo,courseInfo);
        baseMapper.insert(courseInfo);
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setCourseId(courseInfo.getId());
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescriptionService.save(courseDescription);
        return courseInfo.getId();
    }

    @Override
    public CourseFormVo getCourseById(Long id) {
        CourseFormVo courseFormVo = new CourseFormVo();
        Course course = baseMapper.selectById(id);
        BeanUtils.copyProperties(course,courseFormVo);
        CourseDescription courseDescription = courseDescriptionService.getById(course.getId());
        courseFormVo.setDescription(courseDescription.getDescription());
        return courseFormVo;
    }

    @Override
    public Long updateCourseById(CourseFormVo courseFormVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);
        CourseDescription courseDescription = courseDescriptionService.getById(course.getId());
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescriptionService.updateById(courseDescription);
        return course.getId();
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(Long id) {

        return baseMapper.selectCoursePublishVo(id);
    }

    @Override
    public void publishCourse(Long id) {
        Course course = baseMapper.selectById(id);
        course.setStatus(1);
        course.setPublishTime(new Date());
        baseMapper.updateById(course);
    }

    @Override
    public void removeCourseId(Long id) {

        videoService.removeVideoCourseId(id);

        chapterService.removeChapterCourseId(id);

        courseDescriptionService.removeById(id);

       baseMapper.deleteById(id);
    }

    /**
     * 根据course中的id查询相应的名称
     * @param course
     * @return
     */
    private  void getNameById(Course course){
        //根据讲师id查询
            Teacher teacher = teacherService.getById(course.getTeacherId());
            if (teacher != null) {
                course.getParam().put("teacheName",teacher.getRelName());
            }
            //
        Subject subjectParent = subjectService.getById(course.getSubjectParentId());
            if (subjectParent != null) {
                course.getParam().put("subjectParentTitle",subjectParent.getTitle());
            }
         Subject subject = subjectService.getById(course.getSubjectId());
        if (subjectParent != null) {
            course.getParam().put("subjectTitle",subject.getTitle());
         }

    }
}
